#include "base/core/os/mutex.h"
#include <errno.h>
#include "base/core/com_define.h"
#include "base/core/os/debug.h"

bool InitMutex(MUTEX_HANDLE * mutex_handle) 
{
  return pthread_mutex_init(mutex_handle, NULL) == 0;
}

bool DeleteMutex(MUTEX_HANDLE * mutex_handle) 
{
  return pthread_mutex_destroy(mutex_handle) == 0;
}

bool LockMutex(MUTEX_HANDLE * mutex_handle) 
{
  return pthread_mutex_lock(mutex_handle) == 0;
}

bool TryLockMutex(MUTEX_HANDLE * mutex_handle) 
{
	int ret = pthread_mutex_trylock(mutex_handle);
	if (ret != 0) 
	{
		ret = ErrorNO();
		MMO_ASSERT(ret == EBUSY);
		return false;
	}
	return true;
}

bool UnlockMutex(MUTEX_HANDLE * mutex_handle)
{
	MMO_ASSERT_ERROR(mutex_handle, -1, EINVAL);
	return pthread_mutex_unlock(mutex_handle) == 0;
}
